package cn.lixin.org.controller;

import cn.lixin.basic.utils.AjaxResult;
import cn.lixin.basic.utils.PageList;
import cn.lixin.org.domain.Shop;
import cn.lixin.org.query.ShopQuery;
import cn.lixin.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //@Controller+@ResponseBody(转换json)
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "查询所有店铺")
    public List<Shop> queryAll(){
        return shopService.loadAll();
    }

    /**
     * 根据id查询店铺
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个店铺",notes = "根据id查询店铺")
    public Shop queryById(@PathVariable("id") Long id){ // PathVariable：一个{}的时候可以不写
        return shopService.loadById(id);
    }

    /**
     * 高级查询
     * @return
     * RequestBody: 如果请求的是一个对象，则用RequestBody ，请求体
     *  axios get不支持传对象
     */
    @PostMapping
    @ApiOperation(value = "高级查询",notes = "根据query高级查询")
    public PageList<Shop> pageList(@RequestBody ShopQuery query){ // PathVariable：一个{}的时候可以不写
        return shopService.queryPage(query);
    }

    /**
     * 修改添加
     * PutMapping: 修改或添加
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            System.out.println(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 删除
     * DeleteMapping: 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
           shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    /**
     * 删除所有
     *
     * @param ids
     * RequestBody : 数组也是对象
     */
    @PatchMapping
    @ApiOperation(value = "删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult removeList(@RequestBody List<Long> ids){
        try {
            shopService.removeList(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
    /**
     * 查询所有店铺
     */

    @PostMapping("/settlement")
    public AjaxResult Settlement(@RequestBody Shop shop){
        try {
            shopService.Settlement(shop);
            return AjaxResult.me();
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 店铺入驻成功发送邮箱
     * @param email
     * @return
     */
    @GetMapping("/audit")
    @ApiOperation(value = "邮箱",notes = "发送邮箱")
    public AjaxResult Audit(String email){
        try {
            shopService.SendEmail(email);
            return AjaxResult.me();
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


}
